<template>
  <div class="login-container">
    <el-form :model="loginForm" ref="numberValidateForm" label-width="100px" class="demo-ruleForm login-form">
      <h3>登录</h3>
      <el-form-item
          label="账号"
          prop="username"
          :rules="[
      { required: true, message: '账号不能为空'},
      { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
    ]"
      >
        <el-input type="username" v-model="loginForm.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item
          label="密码"
          prop="password"
          :rules="[
      { required: true, message: '密码不能为空'},
      { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
    ]"
      >
        <el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('numberValidateForm')">提交</el-button>
        <el-button @click="resetForm('numberValidateForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: '',
        password:''
      }
    };
  },
  methods: {
    submitForm(formName) {
      const _this=this
      _this.$refs[formName].validate((valid) => {
        if (valid) {
          axios.get("http://localhost:8089/user/login", {params:_this.loginForm}).then(
              function (response) {
                if (response.data.code===0){
                  console.log(response)
                  localStorage.setItem("user",JSON.stringify(response.data.data))
                  localStorage.setItem("isLogin",1);
                  _this.$router.replace({path:'/libraryManager'})
                }else{
                  _this.$alert('账号或密码错误','提示',{
                    confirmButtonText:'确定'
                  })
                }
              }
          )
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
.login-container{
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: space-around;
  background: url("../assets/1.jpg") no-repeat;
}
.login-form{
  -webkit-border-radius: 5px;
  border-radius: 5px;
  margin: auto auto;
  width: 350px;
  padding: 25px 50px 10px;
  background-color: rgba(3, 3, 3, 0.4);
  border: 1px solid #889878;
}
h3{
  text-align: center;
  color: #fff;
}
</style>